package com.yyplatform.admin.infra.utils;

import java.util.StringTokenizer;

import org.apache.commons.lang3.StringUtils;

import com.yyplatform.admin.infra.model.dto.RequestInfoDTO;

import eu.bitwalker.useragentutils.UserAgent;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-14
 */
@Slf4j
public class RequestUtils {


    public static RequestInfoDTO getRequestInfo(HttpServletRequest request) {
        RequestInfoDTO requestInfoDTO = new RequestInfoDTO();

        requestInfoDTO.setUrl(request.getRequestURL().toString());
        requestInfoDTO.setMethod(request.getMethod());
        requestInfoDTO.setUserAgent(getUserAgent(request));
        requestInfoDTO.setIp(getIp(request));
        requestInfoDTO.setLocation(getLocation(request));
        requestInfoDTO.setReferer(getReferer(request));
        requestInfoDTO.setOs(getOs(request));
        requestInfoDTO.setBrowser(getBrowser(request));


        return requestInfoDTO;
    }


    public static String getIp(HttpServletRequest request) {
        String xForwardedForHeader = request.getHeader("X-Forwarded-For");
        if (xForwardedForHeader == null) {
            return request.getRemoteAddr();
        } else {
            // As of https://en.wikipedia.org/wiki/X-Forwarded-For
            // The general format of the field is: X-Forwarded-For: client, proxy1, proxy2 ...
            // we only want the client
            return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
        }
    }

    // todo https://github.com/HaraldWalker/user-agent-utils
    public static String getUserAgent(HttpServletRequest request) {
        // tod 获取ua
        String userAgentStr = request.getHeader("User-Agent");
        if (StringUtils.isEmpty(userAgentStr)) {
            return "";
        }

        UserAgent userAgent = new UserAgent(userAgentStr);
        return userAgentStr;
    }

    public static String getLocation(HttpServletRequest request) {
        // todo 获取location
        return "";
    }

    public static String getOs(HttpServletRequest request) {
        String userAgentStr = request.getHeader("User-Agent");
        if (StringUtils.isEmpty(userAgentStr)) {
            return "";
        }

        UserAgent userAgent = new UserAgent(userAgentStr);
        return userAgent.getOperatingSystem().getName();
    }


    // todo https://github.com/HaraldWalker/user-agent-utils
    public static String getOs2(HttpServletRequest request) {
        // todo 获取os
        String browserDetails = request.getHeader("User-Agent");
        String userAgent = browserDetails;
        String user = userAgent.toLowerCase();

        String os = "";
        String browser = "";

        //        log.info("User Agent for the request is===>" + browserDetails);
        //=================OS=======================
        if (userAgent.toLowerCase().indexOf("windows") >= 0) {
            os = "Windows";
        } else if (userAgent.toLowerCase().indexOf("mac") >= 0) {
            os = "Mac";
        } else if (userAgent.toLowerCase().indexOf("x11") >= 0) {
            os = "Unix";
        } else if (userAgent.toLowerCase().indexOf("android") >= 0) {
            os = "Android";
        } else if (userAgent.toLowerCase().indexOf("iphone") >= 0) {
            os = "IPhone";
        } else {
            os = "UnKnown, More-Info: " + userAgent;
        }
        //===============Browser===========================
        if (user.contains("msie")) {
            String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
            browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
        } else if (user.contains("safari") && user.contains("version")) {
            browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0] + "-"
                    + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
        } else if (user.contains("opr") || user.contains("opera")) {
            if (user.contains("opera")) {
                browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0] + "-"
                        + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
            } else if (user.contains("opr")) {
                browser =
                        ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-")).replace("OPR",
                                "Opera");
            }
        } else if (user.contains("chrome")) {
            browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
        } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) || (
                user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) || (
                user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) {
            //browser=(userAgent.substring(userAgent.indexOf("MSIE")).split(" ")[0]).replace("/", "-");
            browser = "Netscape-?";

        } else if (user.contains("firefox")) {
            browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
        } else if (user.contains("rv")) {
            browser = "IE-" + user.substring(user.indexOf("rv") + 3, user.indexOf(")"));
        } else {
            browser = "UnKnown, More-Info: " + userAgent;
        }
        //        log.info("Operating System======>" + os);
        //        log.info("Browser Name==========>" + browser);
        return os;
    }


    // todo https://github.com/HaraldWalker/user-agent-utils
    public static String getBrowser(HttpServletRequest request) {
        String userAgentStr = request.getHeader("User-Agent");
        if (StringUtils.isEmpty(userAgentStr)) {
            return "";
        }

        UserAgent userAgent = new UserAgent(userAgentStr);
        return userAgent.getBrowser().getName();
    }


    public static String getOrigin(HttpServletRequest request) {
        return null;
    }

    public static String getUrl(HttpServletRequest request) {
        return request.getRequestURL().toString();
    }

    public static String getMethod(HttpServletRequest request) {
        return request.getMethod();
    }


    public static String getReferer(HttpServletRequest request) {
        return "";
    }
}
